<?php

/**
 * 商品处理
 */

namespace app\mall\common;

class MallCommon extends \app\member\common\MemberCommon {


    public function addCart() {
        $data = request('post');
        if (!target('mall/Order', 'service')->addCart($this->userInfo['user_id'], $data)) {
            $this->error(target('mall/Order', 'service')->getError());
        }
        $this->success('添加商品成功!');
    }

    public function addFollow() {
        $data = request('post');

        if (empty($data['mall_id'])) {
            $this->error('商品参数有误!');
        }

        $mallInfo = target('mall/Mall')->getInfo($data['mall_id']);
        if (empty($mallInfo)) {
            $this->error('该商品不存在!');
        }
        if (!$mallInfo['status']) {
            $this->error('该商品已下架!');
        }
        if ($mallInfo['up_time']) {
            if ($mallInfo['up_time'] > time()) {
                $this->error('该商品还未上架!');
            }
        }
        if ($mallInfo['down_time']) {
            if ($mallInfo['down_time'] < time()) {
                $this->error('该商品已下架!');
            }
        }

        $type = target('shop/Shop', 'service')->addFollow('mall', $mallInfo['mall_id'], $this->userInfo['user_id'], $mallInfo['title'], $mallInfo['image'], $mallInfo['sell_price']);
        if (!$type) {
            $this->error(target('shop/Shop', 'service')->getError());
        }

        $msg = '';
        if($type == 'inc') {
            if(!target('mall/Mall')->where(['mall_id' => $data['mall_id']])->setInc('favorite', 1)) {
                $this->error('添加收藏失败！');
            }
            $msg = '收藏商品成功！';
        }

        if($type == 'dec') {
            if(!target('mall/Mall')->where(['mall_id' => $data['mall_id']])->setDec('favorite', 1)) {
                $this->error('取消收藏失败！');
            }
            $msg = '取消收藏成功！';
        }
        $this->success($msg);
    }

    public function addFaq() {
        $id = request('post', 'mall_id', 0, 'intval');
        $info = target('mall/Mall')->getInfo($id);
        if (empty($info) || !$info['status']) {
            $this->error('商品不存在！');
        }
        $msg = target('shop/Shop', 'service')->addFaq('mall', $id, $this->userInfo['user_id'], 0, request('post', 'content'));
        if (!$msg) {
            $this->error(target('shop/Shop', 'service')->getError());
        }
        $this->success($msg, url('mall/Info/index', ['id' => $id]));
    }

}